use std::time::SystemTime;
use chrono::{DateTime, Local, NaiveDateTime, TimeZone};




pub(crate) fn get_now_format_time() -> String{
    get_date_time().format("%Y-%m-%d %H:%M:%S%.3f").to_string()
}
pub(crate) fn get_print_format_time() -> String{
    get_date_time().format("%Y%m%d_%H%M%S").to_string()
}

/* 获取当前的 datetime */
pub(crate) fn get_date_time() -> DateTime<Local> {
    Local.timestamp_nanos(get_system_time_nanos() as i64)
}


pub(crate) fn get_system_time() -> u128 {
    return SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_millis();
}
/* 微妙 */
pub(crate) fn get_system_time_micros() -> u128 {
    return SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_micros();
}

pub(crate) fn get_system_time_nanos() -> u128 {
    return SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_nanos();
}

/**
 * 格式转换
 */
pub fn date_format(date:DateTime<Local>) -> String{
    date.format("%Y-%m-%d %H:%M:%S").to_string()
}

/**
 * str 转换 日期类
 */
pub fn str_to_date(content:&str) -> DateTime<Local>{
    match NaiveDateTime::parse_from_str(content, "%Y-%m-%d %H:%M:%S") {
        Ok(date_time) => {
            return  date_time.and_local_timezone(Local).unwrap()
        }
        Err(_) => {Local::now()}
    }
}


/* 解析英文月份 */


/* 月，日 等时间 补0 */
pub(crate) fn time_0completion(t:&str) -> String{
    if t.len() == 1{
        format!("0{}", t)
    }else {
        t.to_string()
    }
}

/* 解析时间 字符串 */
pub(crate) fn jx_license_time(time:&str) -> Option<String>{
    if time.len() < 15 {
        return  None;
    }
    let list:Vec<String> = time.split_whitespace().map(|s|{s.to_string()}).collect();
    if list.len() > 3{
        // 月份
        let month_str = month_en_num(&list[0]);
        let day_str = time_0completion(&list[1]);

        Some(format!("{}-{}-{} {}", &list[2],month_str,day_str,&list[3]))
    }else {
        None
    }
}


pub(crate) fn month_en_num(enm:&str) -> String{
    match enm {
        "Jan" => {"01".to_string()}
        "Feb" => {"02".to_string()}
        "Mar" => {"03".to_string()}
        "Apr" => {"04".to_string()}
        "May" => {"05".to_string()}
        "Jun" => {"06".to_string()}
        "Jul" => {"07".to_string()}
        "Aug" => {"08".to_string()}
        "Sep" => {"09".to_string()}
        "Oct" => {"10".to_string()}
        "Nov" => {"11".to_string()}
        "Dec" => {"12".to_string()}
        _ => {"".to_string()}
    }
}






